//
//  Shape.m
//  DemoConnectionBetweenShapes
//
//  Created by duc nguyen van on 8/22/11.
//  Copyright 2011 STU. All rights reserved.
//

#import "Shape.h"


@implementation Shape
@synthesize delegate,cnResizeView,EndLine,BeginLine;

#pragma mark -
#pragma mark delegateConnectionRizeView

-(void)panOnThisView:(UIPanGestureRecognizer *)pan andVexter:(CGPoint)vex{
	CGPoint cen = self.center;
	cen.x += vex.x;
	cen.y += vex.y;
	self.center = cen;
	
	if ([EndLine count] > 0) {
		for (ShapeLine *lineView in EndLine) {
			float dentaHeight = 0;
			float dentaWidth = 0;
			lineView.transform = CGAffineTransformMakeRotation(0);
			
			CGPoint newBpoint = [lineView.beginViewPoint.superview convertPoint:lineView.beginViewPoint.center toView:lineView.beginViewPoint.superview.superview.superview];
			CGPoint newEpoint = [self convertPoint:lineView.endViewPoint.center toView:self.superview];
			
			NSLog(@"B is %f and %f , E is %f and %f", newBpoint.x, newBpoint.y, newEpoint.x, newEpoint.y);
			
			if (lineView.layer.anchorPoint.x == 1 && lineView.layer.anchorPoint.y == 0.5) {
				lineView.layer.anchorPoint = CGPointMake(0.0, 0.5);
				
				dentaHeight = newEpoint.y - newBpoint.y;
				dentaWidth = newEpoint.x - newBpoint.x;
			}
			
			CGPoint temp = lineView.center;
			temp.x -= dentaWidth;
			temp.y -= dentaHeight;
			lineView.center = temp;
			
			//CGPoint newEpoint = [self convertPoint:lineView.endViewPoint.center toView:self.superview];
			//CGPoint newBpoint = lineView.frame.origin;
			
			float width_view = sqrt(pow((newEpoint.x - newBpoint.x),2) + pow((newEpoint.y - newBpoint.y),2));
			float rotationValue = atan((newEpoint.y - newBpoint.y) / (newEpoint.x - newBpoint.x));
			
			if (newEpoint.x < newBpoint.x) {
				rotationValue += M_PI;
			}
			
			lineView.frame = CGRectMake(lineView.frame.origin.x, lineView.frame.origin.y, width_view, lineView.frame.size.height);
			lineView.transform = CGAffineTransformMakeRotation(rotationValue);
		}
	}
	
	if ([BeginLine count] > 0) {
		for (ShapeLine *lineView in BeginLine) {
			float dentaHeight = 0;
			lineView.transform = CGAffineTransformMakeRotation(0);
			
			CGPoint newBpoint = [self convertPoint:lineView.beginViewPoint.center toView:self.superview];
			CGPoint newEpoint = [lineView.endViewPoint.superview convertPoint:lineView.endViewPoint.center toView:lineView.endViewPoint.superview.superview.superview];
			
			if (lineView.layer.anchorPoint.x == 0.0 && lineView.layer.anchorPoint.y == 0.5) {
				lineView.layer.anchorPoint = CGPointMake(1, 0.5);
				dentaHeight = newEpoint.y - newBpoint.y;
			}
		
			float dentaWidth = newEpoint.x - (lineView.frame.origin.x + lineView.frame.size.width);
			float dentaRotation = atan((newEpoint.y - newBpoint.y) / (newEpoint.x - newBpoint.x));
				
			CGPoint temp = lineView.center;
			temp.x += dentaWidth;
			temp.y += dentaHeight;
			lineView.center = temp;
			
			//set frame
			float needWidth = newEpoint.x - newBpoint.x;
			float needHeight = newEpoint.y - newBpoint.y;
			
			float frameWidth = sqrt(pow(needWidth, 2) + pow(needHeight, 2));
			lineView.frame  = CGRectMake(lineView.frame.origin.x + vex.x, lineView.frame.origin.y, frameWidth, lineView.frame.size.height);
			
			if (newEpoint.x < newBpoint.x) {
				dentaRotation += M_PI;
			}
			
			lineView.transform = CGAffineTransformMakeRotation(dentaRotation);
		}
	}
}

-(void)moveWithPan:(UIPanGestureRecognizer *)pan andID:(int)intID{
	
}

-(void)tapOnView:(UITapGestureRecognizer *)tap{
	//selectedView = tap.view;
	NSLog(@"tapped on view");
}

-(void)tapOnSubView:(UITapGestureRecognizer *)tap andSize:(CGSize)size andSubView:(UIView*)SView{		
	[delegate sendingTapOnSubView:tap andSize:size andSubView:SView];
}

#pragma mark -
#pragma mark init

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
		float percent = 30;
		cnResizeView = [[ConnectionResizeView alloc] initWithFrame:CGRectMake(0, 0,frame.size.width,frame.size.height)];
		cnResizeView.delegate = self;
		
		shapeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,
															 frame.size.width - (frame.size.width * percent)/100,
															 frame.size.height - (frame.size.height * percent)/100)];
		shapeView.backgroundColor = [UIColor greenColor];
		shapeView.center = CGPointMake(self.frame.size.width / 2,self.frame.size.height / 2);
		
		[self addSubview:shapeView];
		[self addSubview:cnResizeView];
		
		EndLine = [[NSMutableArray alloc] init];
		BeginLine = [[NSMutableArray alloc] init];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
    [super dealloc];
}


@end
